package com.lxq.smartsign.client.util;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lxq.smartsign.client.R;
import com.lxq.smartsign.client.databinding.ItemAttendanceBinding;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class AttendanceAdapter extends RecyclerView.Adapter<AttendanceAdapter.ViewHolder> {

    JSONArray array;
    public AttendanceAdapter(JSONArray array){
        this.array=array;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_attendance,null));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        try {
            JSONObject itemJson=array.getJSONObject(position);
            holder.swipeType.setText(itemJson.optString("swipeType"));
            if (!itemJson.optString("negative").equals("无")){
                holder.negative.setText(itemJson.optString("negative"));
                holder.negative.setVisibility(View.VISIBLE);
            }
            holder.swipeWay.setText(itemJson.optString("swipeDevice")+"打卡");
            String formattedTime=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒", Locale.CHINA)
                    .format(new Date(itemJson.optLong("swipeTime")));
            holder.swipeTime.setText(formattedTime);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return array.length();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView swipeType;
        TextView swipeTime;
        TextView swipeWay;
        TextView negative;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            swipeTime=itemView.findViewById(R.id.item_swipe_time);
            swipeType=itemView.findViewById(R.id.item_swipe_type);
            swipeWay=itemView.findViewById(R.id.item_swipe_way);
            negative=itemView.findViewById(R.id.item_swipe_negative);
        }
    }
}
